﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace LiUTrading.Models
{
    [MetadataType(typeof(Ad_Validation))]
    public partial class Ad
    {
        public bool IsOwnedBy(string userName)
        {
            return String.Equals(UserLiuId, userName, StringComparison.Ordinal);
        }

    }

    public class Ad_Validation
    {
        [Required(ErrorMessage = "Title is required")]
        [StringLength(20, ErrorMessage = "Title may not be longer than 20 characteds")]
        public string Title { get; set; }

        [Required(ErrorMessage = "Info text is required")]
        [StringLength(265, ErrorMessage = "Info text may not be longer than 256 characters")]
        public string InfoText { get; set; }
                
        [Required(ErrorMessage = "Price is required")]
        [PosNumberNoZero(ErrorMessage = "Price needs to be greater than zero")]
        public int Price
        {
            get;
            set;
        }

        public string UserId { get; set; }

        public string UserLiuId { get; set; }
    }

    public class PosNumberNoZeroAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }
            int getal;
            if (int.TryParse(value.ToString(), out getal))
            {

                if (getal == 0)
                    return false;

                if (getal > 0)
                    return true;
            }
            return false;

        }
    }

}